#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

struct node{
	int i,j,mode,cnt;
};

int di[]={0,0,1,-1};
int dj[]={1,-1,0,0};

int n,m,si,sj,gi,gj,res=-1;
void bfs(vector<vector<char>>& g){
	vector<vector<vector<bool>>> visited(n,vector<vector<bool>>(m,vector<bool>(2,false)));
	queue<node> q;
	q.push({si,sj,0,0});
	while(!q.empty()){
		node p=q.front();
		// cout<<p.i<<' '<<p.j<<' '<<p.mode<<'\n';
		q.pop();
		if(g[p.i][p.j]=='G'){
			res=p.cnt;
			return;
		}
		for(int i=0;i<4;i++){
			int ni=p.i+di[i];
			int nj=p.j+dj[i];
			if(ni<0||ni>=n||nj<0||nj>=m)continue;
			if(p.mode==0&&g[ni][nj]=='x')continue;
			if(p.mode==1&&g[ni][nj]=='o')continue;
			if(g[ni][nj]=='#')continue;
			if(g[ni][nj]=='?')p.mode^=1;
			else q.push({ni,nj,p.mode,p.cnt+1});
			if(!visited[ni][nj][p.mode]){
                visited[ni][nj][p.mode] = true;
                q.push({ni,nj,p.mode,p.cnt+1});
            }
		}
	}
}

void solve(){
    cin>>n>>m;
    vector<vector<char>> g(n,vector<char>(m));
    for(int i=0;i<n;i++){
    	for(int j=0;j<m;j++){
    		cin>>g[i][j];
    		if(g[i][j]=='S'){
    			si=i;
    			sj=j;
    		}if(g[i][j]=='G'){
    			gi=i;
    			gj=j;
    		}
    	}
    }
    bfs(g);
    cout<<res;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}